package crawler;

import java.net.URL;
import java.util.Scanner;

/*
 * This program sends url's to the WikiPageScraper, and gives the resulting data 
 * to the QuestionGenerator
 * 
 * @author James Jensen
 * @version February 7, 2012
 */

public class CrawlerControl {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String pageUrl;
		
		/************* For testing purposes only *************/
		pageUrl = "https://en.wikipedia.org/wiki/Ibm";
		generateQuestions(pageUrl);
		
		pageUrl = "https://en.wikipedia.org/wiki/Alan_Turing";
		generateQuestions(pageUrl);
		
		pageUrl = "https://en.wikipedia.org/wiki/Dennis_Ritchie";
		generateQuestions(pageUrl);
		
		pageUrl = "https://en.wikipedia.org/wiki/Ken_Thompson";
		generateQuestions(pageUrl);
		
		pageUrl = "https://en.wikipedia.org/wiki/Apple_Computer";
		generateQuestions(pageUrl);
		
		pageUrl = "https://en.wikipedia.org/wiki/Microsoft";
		generateQuestions(pageUrl);
		
		pageUrl = "https://en.wikipedia.org/wiki/Intel";
		generateQuestions(pageUrl);
		
		pageUrl = "https://en.wikipedia.org/wiki/Linux";
		generateQuestions(pageUrl);
		
		pageUrl = "https://en.wikipedia.org/wiki/C_(programming_language)";
		generateQuestions(pageUrl);
		
		pageUrl = "https://en.wikipedia.org/wiki/Python_(programming_language)";
		generateQuestions(pageUrl);
		
		pageUrl = "https://en.wikipedia.org/wiki/Ruby_(programming_language)";
		generateQuestions(pageUrl);
		
		pageUrl = "https://en.wikipedia.org/wiki/Java_programming_language";
		generateQuestions(pageUrl);
		
		pageUrl = "https://en.wikipedia.org/wiki/C%2B%2B";
		generateQuestions(pageUrl);
		
		pageUrl = "https://en.wikipedia.org/wiki/ALGOL_58";
		generateQuestions(pageUrl);
		/******************************************************/
		
		System.out.println("Done.");
	}
	
	private static String getHtmlSource(String url) {
        String htmlPage = "";
        URL site;
		try {
			site = new URL(url);
			Scanner input;
			input = new Scanner(site.openStream());

			while (input.hasNextLine()) {
				// Line of words
				htmlPage += input.nextLine();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

        return htmlPage;
	}
	
	private static void generateQuestions(String url) {		
		WikiPageScraper scraper = new WikiPageScraper( getHtmlSource(url) );
		QuestionGenerator generator = new QuestionGenerator( url, scraper.getTitle(), scraper.getTableInfo() , scraper.getSentences() );
	}

}
